package com.sphd.emanu.activity.search;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.IdRes;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.sphd.emanu.R;
import com.sphd.emanu.activity.moneymessage.MoneyMessageActivity;
import com.sphd.emanu.config.Constant;
import com.sphd.emanu.mvp.MVPBaseActivity;
import com.sphd.emanu.view.shade.FanImageView;
import com.wzutils.L;
import com.wzutils.TimeUtils;

import org.chenglei.widget.datepicker.DatePicker;
import org.chenglei.widget.datepicker.Sound;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 搜索界面
 *
 * @Author Hu
 * @Time 2017/7/17 -- 16:07.
 * @Email 87683202@qq.com
 */
public class SearchActivity extends MVPBaseActivity<SearchContract.View, SearchPresenter> implements SearchContract.View {

    @BindView(R.id.toolbar)
    Toolbar toolbar;
    @BindView(R.id.balance_radiobutton)
    RadioButton balanceRadiobutton;
    @BindView(R.id.in_radiobutton)
    RadioButton inRadiobutton;
    @BindView(R.id.out_radiobutton)
    RadioButton outRadiobutton;
    @BindView(R.id.money_radio_group)
    RadioGroup moneyRadioGroup;
    @BindView(R.id.start_time_textview)
    TextView startTimeTextview;
    @BindView(R.id.end_time_textview)
    TextView endTimeTextview;
    @BindView(R.id.start_radiobutton)
    RadioButton startRadiobutton;
    @BindView(R.id.end_radiobutton)
    RadioButton endRadiobutton;
    @BindView(R.id.time_radio_group)
    RadioGroup timeRadioGroup;
    @BindView(R.id.date_picker1)
    DatePicker datePicker1;
    @BindView(R.id.ok_time_image)
    ImageView okTimeImage;
    @BindView(R.id.search_image)
    FanImageView searchImage;
    Intent intent;
    Bundle bundle;
    int searchFlag = R.id.balance_radiobutton;//将余额的id赋予初始化的flag 因为初始化的时候余额是被选中的

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_search);
        ButterKnife.bind(this);
        initView();
        initEvent();
    }

    private void initView() {
        intent = getIntent();
        bundle = intent.getExtras();
        setTitleBar(toolbar, "查询");
        Sound sound1 = new Sound(this);
        datePicker1.setSoundEffect(sound1)
                .setTextColor(Color.BLACK)
                .setFlagTextColor(Color.BLACK)
                .setBackground(Color.WHITE)
                .setTextSize(40)
                .setFlagTextSize(16)
                .setSoundEffectsEnabled(true);


    }

    private void initEvent() {
        toolbar.setNavigationOnClickListener((v -> {
            finish();
        }));
        moneyRadioGroup.setOnCheckedChangeListener((group, checkedId) -> {

                    switch (checkedId) {
                        case R.id.balance_radiobutton:
                            searchFlag = R.id.balance_radiobutton;
                            setBackground(balanceRadiobutton, true);
                            setBackground(inRadiobutton, false);
                            setBackground(outRadiobutton, false);
                            bundle.putBoolean(Constant.ShowIncome, true);
                            bundle.putBoolean(Constant.ShowExpend, true);

                            break;
                        case R.id.in_radiobutton:
                            searchFlag = R.id.in_radiobutton;
                            setBackground(balanceRadiobutton, false);
                            setBackground(inRadiobutton, true);
                            setBackground(outRadiobutton, false);
                            bundle.putBoolean(Constant.ShowIncome, true);
                            bundle.putBoolean(Constant.ShowExpend, false);
                            break;
                        case R.id.out_radiobutton:
                            searchFlag = R.id.out_radiobutton;
                            setBackground(balanceRadiobutton, false);
                            setBackground(inRadiobutton, false);
                            setBackground(outRadiobutton, true);
                            bundle.putBoolean(Constant.ShowIncome, false);
                            bundle.putBoolean(Constant.ShowExpend, true);
                            break;
                    }
                }
        );
        timeRadioGroup.setOnCheckedChangeListener((group, checkedId) -> {
            switch (checkedId) {
                case R.id.start_radiobutton:
                    setBackground(startRadiobutton, true);
                    setBackground(endRadiobutton, false);
                    break;
                case R.id.end_radiobutton:
                    setBackground(startRadiobutton, false);
                    setBackground(endRadiobutton, true);
                    break;
            }

        });

    }

    /**
     * @param radiobutton 按键
     * @param b           状态字段
     */
    private void setBackground(RadioButton radiobutton, boolean b) {
        radiobutton.setBackgroundResource(b ? R.drawable.radio_check_true : R.drawable.radio_check_false);
        radiobutton.setTextColor(ContextCompat.getColor(context, b ? R.color.colorPrimary : R.color.white));
    }

    @OnClick({R.id.ok_time_image, R.id.search_image})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.ok_time_image:
                if (timeRadioGroup.getCheckedRadioButtonId() == R.id.start_radiobutton) {

                    if (endTimeTextview.getText().toString().equals("请选择")) {
                        startTimeTextview.setText(datePicker1.getYear() + "-" + datePicker1.getMonth() +
                                "-" + datePicker1.getDayOfMonth());
                    } else {
                        int compare = TimeUtils.compare_date(
                                (datePicker1.getYear() + "-" + datePicker1.getMonth() +
                                        "-" + datePicker1.getDayOfMonth()).toString(), endTimeTextview.getText().toString().trim());
                        L.e("------------" + compare);
                        if (compare == 1 || compare == 0) {
                            startTimeTextview.setText(datePicker1.getYear() + "-" + datePicker1.getMonth() +
                                    "-" + datePicker1.getDayOfMonth());
                        } else if (compare == -1) {
                            toast("开始时间不能晚于结束时间");
                        } else if (compare == -2) {

                            toast("时间选择错误，请稍后重试。");
                        }
                    }

                }
                if (timeRadioGroup.getCheckedRadioButtonId() == R.id.end_radiobutton) {
                    if (startTimeTextview.getText().toString().equals("请选择")) {
                        toast("请先选择开始时间。");
                    } else {
                        int compare = TimeUtils.compare_date(startTimeTextview.getText().toString().trim(),
                                (datePicker1.getYear() + "-" + datePicker1.getMonth() +
                                        "-" + datePicker1.getDayOfMonth()).toString());
                        L.e("------------" + compare);
                        if (compare == 1 || compare == 0) {
                            endTimeTextview.setText(datePicker1.getYear() + "-" + datePicker1.getMonth() +
                                    "-" + datePicker1.getDayOfMonth());
                        } else if (compare == -1) {
                            toast("结束时间不能早于开始时间");
                        } else if (compare == -2) {

                            toast("时间选择错误，请稍后重试。");
                        }
                    }

                }

                break;
            case R.id.search_image:
                if (startTimeTextview.getText().toString().equals("请选择") || endTimeTextview.getText().toString().equals("请选择")) {
                    toast("注意,填完整信息");
                } else {
                    bundle.putInt(Constant.R_ID, R.id.search_image);
                    bundle.putString(Constant.Salary_User_Start, startTimeTextview.getText().toString());
                    bundle.putString(Constant.Salary_User_End, endTimeTextview.getText().toString());
                    bundle.putString(Constant.TimeState, "4");
                    intent.setClass(SearchActivity.this, MoneyMessageActivity.class);
                    intent.putExtras(bundle);
                    startActivity(intent);
                }
                break;
        }
    }
}
